package com.thread.threadfirst.http;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.Date;

@Service
@RestController
public class RestTemplateTest {

    @Autowired
    public RestTemplate restTemplate;

    private String urlNoArguments = "http://localhost:9001/testNoArguments";

    private String urlArguments = "http://localhost:9001/testArguments";

    @RequestMapping(value = "/client",method = RequestMethod.POST)
    public String client(@RequestParam("str") String str){

        System.out.println("============NoArguments get=============");
        ResponseEntity<String> responseEntity= restTemplate.getForEntity(urlNoArguments,String.class);
        String body = responseEntity.getBody();
        HttpHeaders headers = responseEntity.getHeaders();
        HttpStatus statusCode = responseEntity.getStatusCode();
        int statusCodeValue = responseEntity.getStatusCodeValue();
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("body=").append(body).append("<hr>")
                .append("headers=").append(headers).append("<hr>")
                .append("statusCode=").append(statusCode).append("<hr>")
                .append("statusCodeValue=").append(statusCodeValue);
        System.out.println("NoArguments get" + stringBuffer);

        System.out.println("============Arguments get=============");
        ResponseEntity<String> responseEntity1 = restTemplate.getForEntity("http://localhost:9001/testArguments?str={1}",String.class,"风");
        String body1 = responseEntity1.getBody();
        HttpHeaders headers1 = responseEntity1.getHeaders();
        HttpStatus statusCode1 = responseEntity1.getStatusCode();
        int statusCodeValue1 = responseEntity1.getStatusCodeValue();
        StringBuffer stringBuffer1 = new StringBuffer();
        stringBuffer1.append("body=").append(body1).append("<hr>")
                .append("headers=").append(headers1).append("<hr>")
                .append("statusCode=").append(statusCode1).append("<hr>")
                .append("statusCodeValue=").append(statusCodeValue1);
        System.out.println("Arguments get" + stringBuffer1);


//        String result = restTemplate.getForObject(url,String.class,"111111111");
//        System.out.println("result= " + result);

        return stringBuffer1.toString();
    }

//    @ResponseBody
    @RequestMapping(value = "/testNoArguments",method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Object testNoArguments(){
        Role role=new Role();
        role.setId(2L);
        role.setNick("无参");
        role.setGold(6000);
        role.setCreateTime(new Date());
        return role;
    }

    @RequestMapping(value = "/testArguments",method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Object testArguments(String str){
        Role role=new Role();
        role.setId(2L);
        role.setNick("F" + str);
        role.setGold(6000);
        role.setCreateTime(new Date());
        return role;
    }
}
